package com.jk1123.us.common.util;

import java.nio.ByteBuffer;

public class ByteUtil {

    //byte 数组与 long 的相互转换
    public static byte[] long2Bytes(long x) {
        ByteBuffer buffer = ByteBuffer.allocate(8);
        buffer.putLong(0, x);
        return buffer.array();
    }

    public static long bytes2Long(byte[] bytes) {
        ByteBuffer buffer = ByteBuffer.allocate(8);
        buffer.put(bytes, 0, bytes.length);
        buffer.flip();//need flip
        return buffer.getLong();
    }
    public static byte[] int2Bytes(int x) {
        ByteBuffer buffer = ByteBuffer.allocate(4);
        buffer.putInt(0, x);
        return buffer.array();
    }

    public static int bytes2int(byte[] bytes) {
        ByteBuffer buffer = ByteBuffer.allocate(4);
        buffer.put(bytes, 0, bytes.length);
        buffer.flip();//need flip
        return buffer.getInt();
    }
    public static int getInt(byte[] bytes){
        ByteBuffer buffer = ByteBuffer.allocate(4);
        buffer.put(bytes, 0, 4);
        buffer.flip();//need flip
        return buffer.getInt();
    }



    public static byte[] _long2Bytes(long number) {
        long temp = number;
        byte[] b = new byte[8];
        for (int i = 0; i < b.length; i++) {
            b[i] = new Long(temp & 0xff).byteValue();//
            temp = temp >> 8; // 向右移8位
        }
        return b;
    }
    public static long _bytes2Long(byte[] b) {
        long s = 0;
        long s0 = b[0] & 0xff;// 最低位
        long s1 = b[1] & 0xff;
        long s2 = b[2] & 0xff;
        long s3 = b[3] & 0xff;
        long s4 = b[4] & 0xff;// 最低位
        long s5 = b[5] & 0xff;
        long s6 = b[6] & 0xff;
        long s7 = b[7] & 0xff;

        // s0不变
        s1 <<= 8;
        s2 <<= 16;
        s3 <<= 24;
        s4 <<= 8 * 4;
        s5 <<= 8 * 5;
        s6 <<= 8 * 6;
        s7 <<= 8 * 7;
        s = s0 | s1 | s2 | s3 | s4 | s5 | s6 | s7;
        return s;
    }
    /**
     * long数组转化为byte数组
     *
     * @param longArray
     * @return
     */
    public static byte[] longArray2Bytes(Long[] longArray){
        byte[] byteArray=new byte[longArray.length*8];
        for(int i=0;i<longArray.length;i++)
        {
            byteArray[0+8*i]=(byte)(longArray[i]>>56);
            byteArray[1+8*i]=(byte)(longArray[i]>>48);
            byteArray[2+8*i]=(byte)(longArray[i]>>40);
            byteArray[3+8*i]=(byte)(longArray[i]>>32);
            byteArray[4+8*i]=(byte)(longArray[i]>>24);
            byteArray[5+8*i]=(byte)(longArray[i]>>16);
            byteArray[6+8*i]=(byte)(longArray[i]>>8);
            byteArray[7+8*i]=(byte)(longArray[i]>>0);
        }
        return byteArray;
    }

    /**
     * byte数组转化为long数组
     *
     * @param byteArray
     * @return
     */
    public static long[] bytes2LongArray(byte[] byteArray){

        long [] longArray=new long[byteArray.length/8];
        for(int i=0;i<longArray.length;i++)
        {
            longArray[i]=(((long)byteArray[0+8*i]&0xff)<<56)
                    |(((long)byteArray[1+8*i]&0xff)<<48)
                    |(((long)byteArray[2+8*i]&0xff)<<40)
                    |(((long)byteArray[3+8*i]&0xff)<<32)
                    |(((long)byteArray[4+8*i]&0xff)<<24)
                    |(((long)byteArray[5+8*i]&0xff)<<16)
                    |(((long)byteArray[6+8*i]&0xff)<<8)
                    |(((long)byteArray[7+8*i]&0xff)<<0);

        }
        return longArray;
    }
}
